/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.veterinarySystem.view;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dsw2.veterinarySystem.bl.DuenoMascotaBlLocal;
import mx.its.dsw2.veterinarySystem.bl.MascotaBlLocal;
import mx.its.dsw2.veterinarySystem.enumeraciones.EnumAmbiente;
import mx.its.dsw2.veterinarySystem.enumeraciones.EnumEspecies;
import mx.its.dsw2.veterinarySystem.enumeraciones.EnumSexo;
import mx.its.dsw2.veterinarySystem.modelo.DuenoMascota;
import mx.its.dsw2.veterinarySystem.modelo.Mascota;

/**
 *
 * @author 100101
 */
@ManagedBean
@SessionScoped
public class PetBean {
    @EJB
    private DuenoMascotaBlLocal duenoMascotaBl;
    @EJB
    private MascotaBlLocal mascotaBl;
    
    private List<Mascota> lista;
    private List<DuenoMascota> listaDuenoMascota;
    private List<String> listaSexo;
    private List<String> listaEspecies;
    private List<String> listaAmbiente;
    private Mascota pet;
    private String accion="";
    private int idDuenoMascota;
    /**
     * Creates a new instance of PetBean
     */
    
    public PetBean() {
        pet=new Mascota();
    }
   
    public Mascota getPet() {
        return pet;
    }

    public void setPet(Mascota pet) {
        this.pet = pet;
    }

    public int getIdDuenoMascota() {
        return idDuenoMascota;
    }

    public void setIdDuenoMascota(int idDuenoMascota) {
        this.idDuenoMascota = idDuenoMascota;
    }

    public List<Mascota> getLista() {
        lista=mascotaBl.getAll();
        return lista;
    }

    public List<String> getListaSexo() {
        listaSexo=new ArrayList<String>();
        EnumSexo[] listaItems= EnumSexo.values();
        for(int x=0;x<listaItems.length;x++){
             listaSexo.add(listaItems[x].name());
         }
        return listaSexo;
    }

    public List<String> getListaEspecies() {
        listaEspecies=new ArrayList<String>();
        EnumEspecies[] listEspecies=EnumEspecies.values();
        for(int x=0;x<listEspecies.length;x++){
             listaEspecies.add(listEspecies[x].name());
         }
        return listaEspecies;
    }

    public List<String> getListaAmbiente() {
        listaAmbiente=new ArrayList<String>();
        EnumAmbiente[] listAmb=EnumAmbiente.values();
        for(int x=0;x<listAmb.length;x++){
             listaAmbiente.add(listAmb[x].name());
         }
        return listaAmbiente;
    }
    
    
    
    public String procesarAccion(){
        if(accion.equalsIgnoreCase("Nuevo")){
        mascotaBl.register(pet);
        }else{
            if(accion.equalsIgnoreCase("Editar")){
               mascotaBl.modify(pet); 
            }else{
                if(accion.equalsIgnoreCase("Eliminar")){
                    mascotaBl.remove(pet);
                }
            }
        }
        accion=" ";
        return "ListaMascotas";
    }
    
    public void prepararNuevo(ActionEvent evt){
        accion="Nuevo";
        pet=new Mascota();  
    }
    
     public void prepararEditar(ActionEvent evt){
        accion="Editar";
        int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        pet=lista.get(index);
    }
    
    public void prepararEliminar(ActionEvent evt){
        accion="Eliminar";
        int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        pet=lista.get(index);
    }

    public List<DuenoMascota> getListaDuenoMascota() {
        listaDuenoMascota=duenoMascotaBl.getAll();
        return listaDuenoMascota;
    }
    
    public String prepararCancelar(){
        String link="";
        if(accion.equalsIgnoreCase("Nuevo")||accion.equalsIgnoreCase("Editar")){           
            link="PetCreateEdit";           
        }else{
            if(accion.equalsIgnoreCase("Eliminar")){
                link="ListaMascotas";
            }
        }
        return link;
    }
    
    public String accionAceptar(){
        pet.setIdDuenoMascota(duenoMascotaBl.getDuenoMascotaPorId(idDuenoMascota));
        return "PetConfirmar";
    }
       
}

